<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>
	<p:param type="output" name="data"/>
	<p:param type="output" name="instance"/>

	<p:choose href="#instance">
		<p:when test="not(exists(/null))">
			<!--Get  temporary Session-->
			<p:processor name="oxf:pipeline">
				<p:input name="config" href="../session.xpl"/>
				<p:output name="data" id="tmp_session"/>
			</p:processor>

			<p:processor name="oxf:xslt">
				<p:input name="instance" href="#instance"/>
				<p:input name="data" href="#tmp_session"/>
				<p:output name="data" id="new_session"/>
				<p:input name="config">
					<xsl:stylesheet version="2.0">
						<xsl:template match="/">
							<xsl:copy>
								<xsl:apply-templates/>
							</xsl:copy>
						</xsl:template>
						<xsl:template match="lang">
							<lang>
								<xsl:if test="doc('input:instance')/preferences/lang/text() != ''">
									<xsl:value-of select="doc('input:instance')/preferences/lang/text()"/>
								</xsl:if>
								<xsl:if test="doc('input:instance')/preferences/lang/text() = ''">
									<xsl:value-of select="."/>
								</xsl:if>
							</lang>
						</xsl:template>
						<xsl:template match="@*|node()" priority="-1">
							<xsl:copy>
								<xsl:apply-templates select="@*|node()"/>
							</xsl:copy>
						</xsl:template>
					</xsl:stylesheet>
				</p:input>
			</p:processor>

			<p:processor name="oxf:xslt">
				<p:input name="data" href="#instance"/>
				<p:input name="session" href="#new_session"/>
				<p:output name="data" id="pass_check"/>
				<p:input name="config">
					<xdb:query xsl:version="2.0" collection="/db/portal">
						<xsl:text>xquery version "1.0";</xsl:text>
let $user := /users/user[@id='<xsl:value-of select="doc('input:session')/session/username"/>'] return 
if ('<xsl:value-of select="/preferences/password/new"/>' != '') then
if ($user/password/text() = string(util:md5(string('<xsl:value-of select="preferences/password/old/text()"/>')))) then (
update value $user/password/text() with util:md5(string('<xsl:value-of select="preferences/password/new/text()"/>')),
						<message>Your preferences were successfuly changed.</message>,
update replace $user/lang with <lang>
							<xsl:value-of select="doc('input:session')/session/lang"/>
						</lang>)
else <error>Wrong old password. Please try again.</error> else (
update replace $user/lang with <lang>
							<xsl:value-of select="doc('input:session')/session/lang"/>
						</lang>,<message>Your preferences were successfuly changed.</message>)
					</xdb:query>
				</p:input>
			</p:processor>

			<p:processor name="oxf:xmldb-query">
				<p:input name="datasource" href="/config/datasource.xml"/>
				<p:input name="query" href="#pass_check"/>
				<p:output name="data" id="message"/>
			</p:processor>

			<p:processor name="oxf:scope-serializer">
				<p:input name="config">
					<config>
						<key>session</key>
						<scope>session</scope>
						<session-scope>application</session-scope>
					</config>
				</p:input>
				<p:input name="data" href="#new_session"/>
			</p:processor>

			<p:processor name="oxf:identity">
				<p:input name="data" href="#new_session"/>
				<p:output name="data" id="session"/>
			</p:processor>
			<!--<p:processor name="oxf:redirect">
				<p:input name="data">
					<redirect-url>
						<path-info>/portal/preferences/</path-info>
					</redirect-url>
				</p:input>
			</p:processor>-->
		</p:when>
		<p:otherwise>
			<p:processor name="oxf:identity">
				<p:input name="data">
					<null/>
				</p:input>
				<p:output name="data" id="message"/>
			</p:processor>

			<!--Get Session-->
			<p:processor name="oxf:pipeline">
				<p:input name="config" href="../session.xpl"/>
				<p:output name="data" id="session"/>
			</p:processor>
		</p:otherwise>
	</p:choose>


	<p:processor name="oxf:identity">
		<p:input name="data">
			<password>
				<old/>
				<new/>
				<confirm/>
			</password>
		</p:input>
		<p:output name="data" id="password"/>
	</p:processor>

	<!--read global parameters-->
	<p:processor name="oxf:identity">
		<p:input name="data" href="/config/config.xml"/>
		<p:output name="data" id="global_pref"/>
	</p:processor>

	<!--load translation-->
	<p:processor name="oxf:pipeline">
		<p:input name="data" href="#session#xpointer(/session/lang)"/>
		<p:input name="config" href="/apps/engine/utils/XMLtranslation.xpl"/>
		<p:output name="data" id="translation"/>
	</p:processor>

	<!--Get Cop1-->
	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query">
			<xdb:query collection="/db/portal">
				<cop1>{/cops/cop[@id='cop1']/text()}</cop1>
			</xdb:query>
		</p:input>
		<p:output name="data" id="cop"/>
	</p:processor>

	<!--Create data-->
	<p:processor name="oxf:unsafe-xslt">
		<p:input name="data" href="#session"/>
		<p:input name="cop" href="#cop"/>
		<p:input name="config">
			<data xsl:version="2.0">
				<username>
					<xsl:value-of select="/session/username"/>
				</username>
				<copName>
					<xsl:value-of select="doc('input:cop')"/>
				</copName>
				<serviceBrowserUri use="false"/>
				<widgetManagement>
					<xsl:choose>
						<xsl:when test="/session/level = 2">
							<xsl:text>true</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>false</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
				</widgetManagement>
			</data>
		</p:input>
		<p:output name="data" id="datas"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('data', #datas#xpointer(data/*), #message, #global_pref#xpointer(/globalpref/appname), #translation)"/>
		<p:output name="data" ref="data"/>
	</p:processor>

	<p:processor name="oxf:identity">
		<p:input name="data" href="aggregate('preferences', #password, #session#xpointer(/session/lang))"/>
		<p:output name="data" ref="instance"/>
	</p:processor>

</p:config>